package com.katze.boot.hadoop.controller;

import com.katze.boot.hadoop.entity.HadoopEntity;
import com.katze.boot.hadoop.service.FeignHadoopService;
import com.katze.boot.plugins.feign.FeignRequester;
import com.katze.boot.plugins.shiro.annotations.RestControllerMapping;
import com.katze.boot.plugins.shiro.annotations.RestRequestMapping;
import com.katze.common.Message;
import com.katze.common.env.AppEnvironment;
import com.katze.common.http.HttpUtils;
import com.katze.common.http.ResponseDownloadHelper;
import feign.Response;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;

@RestControllerMapping("/hadoop")
public class HadoopController extends FeignRequester<FeignHadoopService> {
    private static final Logger log = LoggerFactory.getLogger(HadoopController.class);

    @RestRequestMapping(value = "/list/{uid}", skip = true)
    public Message list (@PathVariable("uid") String uid, HadoopEntity entity) {
        return execute(uid, (uri, service) -> service.list(uri, entity.getPath(), entity.getStatus()));
    }

    @RestRequestMapping(value = "/{method}/{uid}", method = RequestMethod.POST, desc = "mkdirs:新建文件夹; rename:重命名; move:移动; copy:复制; ")
    public Message post (@PathVariable("uid") String uid, @PathVariable("method") String method, HadoopEntity entity) {
       return execute(uid, (uri, service) -> service.post(uri, method, entity));
    }

    @RestRequestMapping(value = "/delete/{uid}", method = RequestMethod.DELETE, desc = "delete:删除;")
    public Message delete (@PathVariable("uid") String uid, String path) {
        return execute(uid, (uri, service) -> service.delete(uri, path));
    }

    @RestRequestMapping(value = "/upload/{uid}", method = RequestMethod.POST, desc = "上传文件")
    public Message upload (@PathVariable("uid") String uid, MultipartFile file, String target) {
        return execute(uid, (uri, service) -> service.upload(uri, file, target));
    }

    @RestRequestMapping(value = "/download/{uid}", method = RequestMethod.POST, desc = "下载文件")
    public void download (HttpServletResponse response, @PathVariable("uid") String uid, HadoopEntity entity) {
        long start = System.currentTimeMillis(), end = 0;
        String filename = "";
        try (Response templateResponse = execute(uid, (uri, service) -> service.download(uri, entity.getPath()))){
            end = System.currentTimeMillis();
            filename = templateResponse.headers().get(ResponseDownloadHelper.CONTENT_FILENAME).iterator().next();
            Path file = Files.write(AppEnvironment.TEMP_DIR.getPath(filename), IOUtils.toByteArray(templateResponse.body().asInputStream()));
            if (templateResponse.status() == 200) {
                HttpUtils.download(response).load(file.toFile());
            }
        } catch (Throwable e) {
            log.error("{}文件写入本地临时文件失败", AppEnvironment.TEMP_DIR.getPath(filename), e);
        } finally {
            log.info("打包耗时:{}ms, 写入磁盘耗时:{}ms", end - start, System.currentTimeMillis() - end);
        }
    }
}
